package com.echoesnet.echoesshortvideo.Repositories;

import com.echoesnet.echoesshortvideo.Api.HttpMethods;
import com.echoesnet.echoesshortvideo.Api.RequestHeader;
import com.echoesnet.echoesshortvideo.Constant.Api;
import com.echoesnet.echoesshortvideo.Utils.Cryptology.EncryptSHA1;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import io.reactivex.Observer;

/**
 * Copyright (C) 2018 科技发展有限公司
 * 完全享有此软件的著作权，违者必究
 *
 * @author ben
 * @version 1.0
 * @modifier
 * @createDate 2018/4/19 15:56
 * @description
 */
//@Singleton  // informs Dagger that this class should be constructed once
public class LoginRepository
{
    /**
     * 检测手机是否被注册
     * @param phoneNum
     * @param observer
     */
    public void checkUserRegisterState(String phoneNum, Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.PHONE, phoneNum);
        HttpMethods.getInstance().startServerRequest(observer, Api.LOGINC_CHECK_MOBILE, reqMap, true);
    }

    /**
     * 找回密码
     * @param phone
     * @param observer
     */
    public void findPassWord(String phone, String verlfyCode,String  password,Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.PHONE, phone);
        reqMap.put(RequestHeader.CODE, verlfyCode);
        try
        {
            reqMap.put(RequestHeader.PASSWORD, EncryptSHA1.SHA1(password));
        } catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        HttpMethods.getInstance().startServerRequest(observer, Api.LOGINC_FORGETPASSWORD, reqMap, true);
    }

    /**
     * 修改密码
     * @param newPassword
     * @param password
     * @param observer
     */
    public void alertPassWord(String password,String newPassword,Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        try
        {
            reqMap.put(RequestHeader.OLD_PASSWORD, EncryptSHA1.SHA1(password));
            reqMap.put(RequestHeader.PASSWORD, EncryptSHA1.SHA1(newPassword));
        } catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        HttpMethods.getInstance().startServerRequest(observer, Api.LOGINC_MODIFY_PASSWORD, reqMap, true);
    }
    /**
     * 登录
     * @param platform
     * @param observer
     */
    public void weixinLogin(Platform platform, Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.OPEN_ID, platform.getDb().getUserId());
        reqMap.put(RequestHeader.UNIONID, platform.getDb().get("unionid"));
        reqMap.put(RequestHeader.ACCESS_TOKEN, platform.getDb().getToken());
        reqMap.put(RequestHeader.REFRESH_TOKEN, platform.getDb().get("refresh_token"));
        reqMap.put(RequestHeader.DEVICE_TYPE, "ANDROID");

        HttpMethods.getInstance().startServerRequest(observer, Api.LOGINC_WEIXIN_LOGIN, reqMap, true);
    }

    /**
     * 微信登录
     * @param phone
     * @param observer
     */
    public void login(String phone,String  password,Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.PHONE, phone);
        try
        {
            reqMap.put(RequestHeader.PASSWORD, EncryptSHA1.SHA1(password));
        } catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        HttpMethods.getInstance().startServerRequest(observer, Api.LOGINC_LOGIN, reqMap, true);
    }
}
